// pages/selectaddress/selectaddress.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    addressid:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ addressid: parseInt(options.addressid)});
    this.Find_Address();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  Find_Address:function(){
    var that=this;
    var data = {};
    data.act = "Find_Address";
    wx.showLoading({
      title: '加载中...',
    })
    app.Ajax(data, function (json) {
      wx.hideLoading();
      that.setData({ list: json.data });
    });
  },
  selectAddress: function () {
    var that=this;
    wx.chooseAddress({
      success: function (res) {
        var data = {};
        data.province = res.provinceName;
        data.city = res.cityName;
        data.area = res.countyName;
        data.realname = res.userName;
        data.mobile = res.telNumber;
        data.address = res.detailInfo;
        data.act = "Save_Address";
        wx.showLoading({
          title: '正在保存...',
        })
        app.Ajax(data, function (json) {
          wx.hideLoading();
          if(json.state){
            that.setData({ addressid: json.data });
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2];
            prevPage.setData({ addressid: json.data, province: data.province, city: data.city, area: data.area, realname: data.realname, mobile: data.mobile, address: data.address });
            that.Find_Address();
            wx.navigateBack({
              delta: 1
            })
          }else{
            app.showError(json.msg);
          }
        });
      }
    })
  },
  selectOne:function(e){
    var addressid = e.currentTarget.dataset.addressid;
    var realname = e.currentTarget.dataset.realname;
    var mobile = e.currentTarget.dataset.mobile;
    var address = e.currentTarget.dataset.address;
    var province = e.currentTarget.dataset.province;
    var city = e.currentTarget.dataset.city;
    var area = e.currentTarget.dataset.area;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    prevPage.setData({ addressid: addressid, province:province,city:city,area:area, realname: realname,mobile:mobile,address:address});
    wx.navigateBack({
      delta: 1
    })
  }
})